import {
  cancelReserve_service,
  getTimetableRecords_service,
} from "../../graphql/timetableRecord";
import { useLazyQuery, useMutation } from "@apollo/client";
import { resType } from "../../constant";
import { TimetableRecordType } from "../../types/timetableRecord";

export const useGetTimetableRecords = () => {
  const [fun, { loading }] = useLazyQuery(getTimetableRecords_service, {
    fetchPolicy: "network-only",
  });
  const getTimetableRecords = async () => {
    const res = await fun({
      variables: { page: { pageNum: 1, pageSize: 0 } },
    });
    return res.data.gettimetableRecords as resType<TimetableRecordType[]>;
  };
  return {
    getTimetableRecords,
    loading,
  };
};

export const useCancelReserve = () => {
  const [fun, { loading }] = useMutation(cancelReserve_service);

  const cancelReserve = async (id: string) => {
    const res = await fun({
      variables: {
        id,
      },
    });
    return res.data.deleteTimetableRecord as resType;
  };

  return {
    cancelReserve,
    loading,
  };
};
